package com.hqyj.zss.controller;

import com.hqyj.zss.pojo.Menus;
import com.hqyj.zss.pojo.PageData;
import com.hqyj.zss.service.MenusService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Controller
@RequestMapping("/menus")
public class MenusController {
    @Autowired
    private MenusService menusService;

    @RequestMapping("/all")
    public String queryAllMenus(Integer pageNumber, Integer pageSize, ModelMap modelMap ) {

        //处理参数
        int number;
        int size;
        if (pageNumber == null) {
            number = 1; //默认显示第1页
        } else {
            number = pageNumber;
        }
        if (pageSize == null) {
            size = 5; //默认每页显示五条
        } else {
            size = pageSize;
        }
        //调用服务层换回模型数据
        PageData<Menus> pageData = menusService.queryAllMenusByPage(number, size);
        //给视图传递上下文参数
        modelMap.put("pageData", pageData);
        //请求转发到success.jsp
        return "menus/list";
    }
    @RequestMapping("/returnAdd")
    public String returnList(){
        return "menus/add";
    }

    @ResponseBody
    @RequestMapping("/add")
    public String insertMenus(HttpServletRequest req, HttpServletResponse resp) {
        String menusName = req.getParameter("menusName");
        String menusClass = req.getParameter("menusClass");
        double menusCost = Double.valueOf(req.getParameter("menusCost"));
        double menusPrice = Double.valueOf(req.getParameter("menusPrice"));

        Menus menus = new Menus();
        menus.setMenusName(menusName);
        menus.setMenusClass(menusClass);
        menus.setMenusCost(menusCost);
        menus.setMenusPrice(menusPrice);
        int i = menusService.insertMenus(menus);
        if (i>0){
            return "ok";
        }else{
            return "fail";
        }
    }

    @RequestMapping("/queryById")
    public String queryById(int menusId, ModelMap modelMap){
        Menus menus = menusService.queryByMenusId(menusId);
        modelMap.put("menus",menus);
        return "menus/update";
    }
    @ResponseBody
    @RequestMapping("/update")
    public String updateMenus(HttpServletRequest req, HttpServletResponse resp){
        int menusId = Integer.valueOf(req.getParameter("menusId"));
        String menusName = req.getParameter("menusName");
        String menusClass = req.getParameter("menusClass");
        double menusCost = Double.valueOf(req.getParameter("menusCost"));
        double menusPrice = Double.valueOf(req.getParameter("menusPrice"));

        Menus menus = new Menus();
        menus.setMenusId(menusId);
        menus.setMenusName(menusName);
        menus.setMenusClass(menusClass);
        menus.setMenusCost(menusCost);
        menus.setMenusPrice(menusPrice);
        int i = menusService.updateMenus(menus);
        if (i>0){
            return "ok";
        }else{
            return "fail";
        }
    }

    @ResponseBody
    @RequestMapping("/del")
    public String delByMenusId(HttpServletRequest req,HttpServletResponse resp){
        Integer menusId = Integer.valueOf(req.getParameter("menusId"));
//        System.out.println("menusId = " + menusId);
        int i = menusService.delByMenusId(menusId);
        if (i>0){
            return "ok";
        }else{
            return "fail";
        }
    }




//    @RequestMapping("/queryMenusByName")
//    @ResponseBody
//    public String queryMenusByName(Integer pageNumber, Integer pageSize, ModelMap modelMap,HttpServletRequest req,HttpServletResponse resp){
//        String menusName = req.getParameter("menusName");
//            //处理参数
//            int number;
//            int size;
//            if (pageNumber == null) {
//                number = 1; //默认显示第1页
//            } else {
//                number = pageNumber;
//            }
//            if (pageSize == null) {
//                size = 5; //默认每页显示五条
//            } else {
//                size = pageSize;
//            }
//            //调用服务层换回模型数据
//            PageData<Menus> pageData = menusService.queryByMenusName(number, size,menusName);
//            //给视图传递上下文参数
//            modelMap.put("pageData", pageData);
//            System.out.println("pageData = " + pageData);
//            //请求转发到success.jsp
//            return "ok";
//        }
}
